﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DeepEarth.Core.Controls;

namespace DeepEarth.Toolkit.Common.Controls
{
    public class HeaderedTemplatedItem : ListBox
    {
        #region Properties
        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(HeaderedTemplatedItem), null);
        public object Header
        {
            get { return GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(HeaderedTemplatedItem), null);
        public DataTemplate HeaderTemplate
        {
            get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
            set { SetValue(HeaderTemplateProperty, value); }
        }

        public static readonly DependencyProperty HeaderTemplateSelectorProperty = DependencyProperty.Register("HeaderTemplateSelector", typeof(TemplateSelector), typeof(HeaderedTemplatedItem), null);
        public TemplateSelector HeaderTemplateSelector
        {
            get { return (TemplateSelector)GetValue(HeaderTemplateSelectorProperty); }
            set { SetValue(HeaderTemplateSelectorProperty, value); }
        }

        public static readonly DependencyProperty ItemTemplateSelectorProperty = DependencyProperty.Register("ItemTemplateSelector", typeof(TemplateSelector), typeof(HeaderedTemplatedItem), null);
        public TemplateSelector ItemTemplateSelector
        {
            get { return (TemplateSelector)GetValue(ItemTemplateSelectorProperty); }
            set { SetValue(ItemTemplateSelectorProperty, value); }
        }

        public static readonly DependencyProperty ItemSelectedCommandProperty = DependencyProperty.Register("ItemSelectedCommand", typeof(ICommand), typeof(HeaderedTemplatedItem), null);
        public ICommand ItemSelectedCommand
        {
            get { return (ICommand)GetValue(ItemSelectedCommandProperty); }
            set { SetValue(ItemSelectedCommandProperty, value); }
        }

        public static readonly DependencyProperty HideWhenEmptyProperty = DependencyProperty.Register("HideWhenEmpty", typeof(bool), typeof(HeaderedTemplatedItem), new PropertyMetadata(false));
        public bool HideWhenEmpty
        {
            get { return (bool)GetValue(HideWhenEmptyProperty); }
            set { SetValue(HideWhenEmptyProperty, value); }
        }
        #endregion

        public HeaderedTemplatedItem()
        {
            DefaultStyleKey = typeof(HeaderedTemplatedItem);

            AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler((o, e) => HandleSelection()), true);
        }

        void HandleSelection()
        {
            var selectedItem = SelectedItem;

            if (ItemSelectedCommand != null)
                ItemSelectedCommand.Execute(selectedItem);
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            // Enables listbox functionality
            base.PrepareContainerForItemOverride(element, item);

            var template = ItemTemplateSelector.FindTemplate<DataTemplate>(item);

            var presenter = (ListBoxItem)element;
            presenter.ContentTemplate = template;
            presenter.Content = item;
        }

        protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);

            if (HideWhenEmpty)
                Visibility = Items != null && Items.Any() ? Visibility.Visible : Visibility.Collapsed;
        }
    }
}